//要投票得人名，不能错
var name = "顾茜涵";
//等待页面加载完毕之后，删除cookie得时间，建议500ms,
var waitClearCookieTime = 500;
//选中后等待多久开始投票，，该网站有个刷新机制，时间建议4-5秒之间
var waitForButton = 1000;
//投票结束后重新刷新页面得等待时间，建议2秒，如果网络不佳，可适当延迟时间
var refReshUrlAfterButton = 8000;
//最大投票个数，单次
var perMaxNum = 100;
var curNum = 0;
//最大投票个数，总共
var totalMaxNum = 1000;
//还需要测试123
function start()
{
    //显示投票个数
    showNum();
    if ( isByound() ){
        console.log("已经超过投票次数");
        return;
    }

    var Button = getTPButton();
    if ( Button == undefined ){
        console.log("不是投票页面，，刷新页面");
        clearCookieAndRefresh();
    }
    else{
        //判断是否已经存在cookies
        if ( isHasTou() ){
            //清空cookies,然后刷新页面
            //0.5秒后删除cookie，
            setTimeout(function () {
                //投票
                console.log("已经存在cookies，清空cookies,刷新页面");
                clearAllCookie();
                refReshLocal();
            },waitClearCookieTime);
        }
        else{
            //选中指定得名字，
            setSelected(name);
            //*
            setTimeout(function () {
                //alert("点我投票");
                //投票
                Button.click();

                updateLocalNum();

                reloadByTime();
            },waitForButton);
            //*/
        }
    }
}
function reloadByTime(){
    setTimeout(function () {
        //投票
        console.log('reloadByTime');
        refReshLocal();
    },refReshUrlAfterButton);
}
function refReshLocal(){
    window.location.reload();
}
//clear cookies and refresh
function clearCookieAndRefresh(){
    if ( isHasTou() ){
        //清空cookies,然后刷新页面
        //0.5秒后删除cookie，
        setTimeout(function () {
            //投票
            console.log("已经存在cookies，清空cookies,刷新页面");
            clearAllCookie();
            refReshLocal();
        },waitClearCookieTime);
    }
}
//判断是否有browser_id

function isHasTou(){
    var br = window.getCookie('_zvta');
    if( !br ) return false;
    return true;
}
//清除所有得cookies
function clearAllCookie() {
    var domain = '.'+location.host;
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if(keys) {
        for(var i = keys.length; i--;) {
            console.log("删除 cookie =="+ keys[i]);
            window.delCookie(keys[i]);
        }
    }
}
//获取 投票按钮
//如果没有则返回 undefined
function getTPButton(){
    var next_button = document.getElementById('next_button');
    //next_button.click();
    return next_button;
}
//设置选中得人名
function setSelected(name){
    //获取所有得checkbox
    var elements = document.getElementsByClassName("option_cell_box");
    //遍历
    for ( var i = 0;i<elements.length;i++){
        var ele = elements.item(i);
        //var for_value = ele.getAttribute("for");
        //获取姓名
        var p_elements = ele.getElementsByTagName("p");

        if ( p_elements.length >= 1 ){

            //只看第一个
            if (p_elements.item(0).innerText == name){

                ele.click();
            }
        }
    }
}
//更新本地计数器
function updateLocalNum(){
    var key = 'tp_num_'+name;

    var localNum = window.localStorage.getItem(key);
    if ( localNum == undefined ){
        //本地不存在，计数为1
        localNum = 1;
    }
    else{
        localNum++;
    }
    curNum++;
    window.localStorage.setItem(key,localNum);
}
//显示本地计数器
function showNum(){

    var key = 'tp_num_'+name;
    var localNum = window.localStorage.getItem(key);
    if ( !!localNum ){
        console.log("已经投票个数为："+ localNum);
    }
}
//判断是否超过次数
function isByound() {
    var key = 'tp_num_'+name;
    var localNum = window.localStorage.getItem(key);
    if ( !localNum ){
        return false;
    }

    if ( localNum >= totalMaxNum || curNum >= perMaxNum ){

        return true;
    }
    else {
        return  false;
    }
}